package com.ruoyi.web.controller.personnel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.WorkExperience;
import com.ruoyi.personnel.service.IWorkExperienceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 工作经历Controller
 *
 * @author ruoyi
 * @date 2021-02-23
 */
@Api("工作经历")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/personnel/experience" )
public class WorkExperienceController extends BaseController {

    private final IWorkExperienceService iWorkExperienceService;

    /**
     * 查询工作经历列表
     */
    @ApiOperation("查询工作经历列表")
    @PreAuthorize("@ss.hasPermi('personnel:experience:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkExperience workExperience) {
        startPage();
        List<WorkExperience> list = iWorkExperienceService.queryList(workExperience);
        return getDataTable(list);
    }

    /**
     * 导出工作经历列表
     */
    @ApiOperation("导出工作经历列表")
    @PreAuthorize("@ss.hasPermi('personnel:experience:export')" )
    @Log(title = "工作经历" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(WorkExperience workExperience) {
        List<WorkExperience> list = iWorkExperienceService.queryList(workExperience);
        ExcelUtil<WorkExperience> util = new ExcelUtil<WorkExperience>(WorkExperience.class);
        return util.exportExcel(list, "experience" );
    }

    /**
     * 获取工作经历详细信息
     */
    @ApiOperation("获取工作经历详细信息")
    @PreAuthorize("@ss.hasPermi('personnel:experience:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iWorkExperienceService.getById(id));
    }

    /**
     * 新增工作经历
     */
    @ApiOperation("新增工作经历")
    @PreAuthorize("@ss.hasPermi('personnel:experience:add')" )
    @Log(title = "工作经历" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkExperience workExperience) {
        workExperience.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iWorkExperienceService.save(workExperience) ? 1 : 0);
    }

    /**
     * 修改工作经历
     */
    @ApiOperation("修改工作经历")
    @PreAuthorize("@ss.hasPermi('personnel:experience:edit')" )
    @Log(title = "工作经历" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkExperience workExperience) {
        return toAjax(iWorkExperienceService.updateById(workExperience) ? 1 : 0);
    }

    /**
     * 删除工作经历
     */
    @ApiOperation("删除工作经历")
    @PreAuthorize("@ss.hasPermi('personnel:experience:remove')" )
    @Log(title = "工作经历" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iWorkExperienceService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
